package com.tsinghuabigdata.edu.ddmath.module.mycenter;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.tsinghuabigdata.edu.ddmath.MVPModel.RequestListener;
import com.tsinghuabigdata.edu.ddmath.R;
import com.tsinghuabigdata.edu.ddmath.activity.RoboForToolBarActivity;
import com.tsinghuabigdata.edu.ddmath.bean.LoginInfo;
import com.tsinghuabigdata.edu.ddmath.bean.UserDetailinfo;
import com.tsinghuabigdata.edu.ddmath.commons.http.HttpResponse;
import com.tsinghuabigdata.edu.ddmath.module.login.LoginModel;
import com.tsinghuabigdata.edu.ddmath.requestHandler.LoginReqService;
import com.tsinghuabigdata.edu.ddmath.requestHandler.requestImpl.LoginReqImpl;
import com.tsinghuabigdata.edu.ddmath.util.AccountUtils;
import com.tsinghuabigdata.edu.ddmath.util.AlertManager;
import com.tsinghuabigdata.edu.ddmath.view.CustomOptionPicker;
import com.tsinghuabigdata.edu.ddmath.view.GenderSelectorView;
import com.tsinghuabigdata.edu.ddmath.view.MyProgressDialog;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;


/**
 * 个人信息编辑
 * Created by Administrator on 2016/12/14.
 */

public class EditPersonalinfoActivity extends RoboForToolBarActivity implements View.OnClickListener {
    private View root;
    @ViewInject(R.id.bt_done)
    private Button btDone;

    @ViewInject(R.id.genderview)
    private GenderSelectorView genderSelectorView;

    @ViewInject(R.id.et_nickname)
    private EditText etNickname;

    @ViewInject(R.id.et_reallyname)
    private EditText etReallyname;

    @ViewInject(R.id.et_encrollyears)
    private TextView etEnrollyears;

    private MyProgressDialog progressDialog;
    private Context context;

    private LoginReqService loginReqService = new LoginReqImpl();

    private UserDetailinfo userDetailinfo;
    private CustomOptionPicker picker;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_edit_personalinfo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        context = this;
        initView();
        initData();
    }

    @Override
    public void onLeftClick() {
        super.onLeftClick();
        finish();
    }

    private void initView() {
        setLeftTitle("取消", false);
        setTitle("个人信息");
        setBarTextcolor(R.color.white);
        setUserBarBackgroud(true);


        //listener
        btDone.setOnClickListener(this);
        etEnrollyears.setOnClickListener(this);

        progressDialog = new MyProgressDialog(context);
        initDatepicker();

    }

    private void initData() {
        userDetailinfo = AccountUtils.getUserdetailInfo();
        etNickname.setText(userDetailinfo.getNickName());
        etReallyname.setText(userDetailinfo.getReallyName());
        genderSelectorView.setSelGender(userDetailinfo.getSex().equals("女") ? GenderSelectorView.GIRL : GenderSelectorView.BOY);
        etEnrollyears.setText(userDetailinfo.getSerial());

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_done:
                updatePersonalInfo();
                break;
            case R.id.et_encrollyears:
                picker.show();
                break;
        }
    }

    private void initDatepicker() {
        List<String> years = new ArrayList<>();
        int currYear = Calendar.getInstance().get(Calendar.YEAR);
        String pre = "                                                                                                    ";
        String suff = "                                                                                                     ";
        for (int starty = 1990; starty <= currYear; starty++) {
            years.add(pre + starty + "年" + suff);
        }
        String[] strYears = (String[]) years.toArray(new String[years.size()]);
        picker = new CustomOptionPicker(this, strYears);
        picker.setOffset(2);
        if (years.size() > 0) {
            picker.setSelectedIndex(years.size() - 1);
        }
        picker.setTextSize(19);
        picker.setTitleText("输入入学年份");
        picker.setLineVisible(false);
        picker.setTopBackgroundColor(getResources().getColor(R.color.bar_tint));
        picker.setCancelTextColor(getResources().getColor(R.color.white));
        picker.setTitleTextColor(getResources().getColor(R.color.white));
        picker.setSubmitTextColor(getResources().getColor(R.color.white));

        picker.setTextColor(getResources().getColor(R.color.bartitle_text_color));
        picker.setLineColor(getResources().getColor(R.color.color_999999));
        picker.setOnOptionPickListener(new CustomOptionPicker.OnOptionPickListener() {
            @Override
            public void onOptionPicked(String option) {
                etEnrollyears.setText(option.toString().trim());
            }
        });
    }

    private void updatePersonalInfo() {
        String accessToken = AccountUtils.getLoginUser().getAccessToken();
        String accountId = AccountUtils.getLoginUser().getAccountId();

        String nickname = etNickname.getText().toString().trim();
        String reallyName = etReallyname.getText().toString().trim();
        String sex = genderSelectorView.getSelGender() == GenderSelectorView.GIRL ? "female" : "male";
        String serial = etEnrollyears.getText().toString().trim();
        progressDialog.setMessage("修改个人信息中...");
        progressDialog.show();
        new MycenterModel().editPersonalinfo(accessToken, accountId, reallyName, serial, sex, nickname, new RequestListener() {
            @Override
            public void onSuccess(Object res) {
                //成功后，更新本地的个人信息
                LoginInfo loginInfo = AccountUtils.getLoginUser();
                new LoginModel().queryUserdetailInfo(loginInfo.getAccessToken(), loginInfo.getAccountId(), new RequestListener<UserDetailinfo>() {
                    @Override
                    public void onSuccess(UserDetailinfo res) {
                        if (res != null) {
                            AccountUtils.setUserdetailInfo(res);
                        }
                        progressDialog.dismiss();
                        AlertManager.toast(context, "修改成功");
                        finish();
                    }

                    @Override
                    public void onFail(HttpResponse response, Exception ex) {
                        progressDialog.dismiss();
                        AlertManager.toast(context, "修改失败，请重试");
                    }
                });
            }

            @Override
            public void onFail(HttpResponse response, Exception ex) {
                progressDialog.dismiss();
                AlertManager.showErrorInfo(context, ex);
            }
        });
    }
}
